{% extends 'layout/main.html' %}
{% from 'macros/_macros.html' import render_pagination %}

{% block content_header %}
<div class="row page-titles">
  <div class="col-md-5 col-8 align-self-center">
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="/">首页</a></li>
      <li class="breadcrumb-item active">单位列表</li>
    </ol>
  </div>
</div>
{% endblock %}



{% block content %}
<style>
  .checkbox {
    margin-top: 0px;
    height: 20px;
  }
</style>
<div class="row">
  {% include 'admin/companys/_left.html' %}
  <div class="col" style="overflow: hidden">
    <div class="card">
      <!-- /.box-header -->
      <div class="card-body">

        <div class="table-responsive">
        <table id="dataTable" class="table table-hover table-bordered m-t-10 text-nowrap">
          <thead>
            <tr>
              <th style="width: 10px">
                <div class="checkbox">
                  <input class="ids" type="checkbox" id="check_box">
                  <label for="check_box"></label>
                </div>
              </th>
              <th>单位名称</th>
              <th width="100">报送</th>
              <th width="100">录用</th>
              <th width="100">未录用</th>
              <th width="100">积分</th>
              <th style="width: 40px">操作</th>
            </tr>
          </thead>
          <tbody>
            {% if model %}
            {% for item in model %}
            <tr>
              <td>
                <div class="checkbox">
                  <input class="ids" type="checkbox" id="checkbox{{item.id}}"  data-rid="{{ item.rid }}" data-title="{{ item.title }}"  value="{{item.id}}" name="ids[]">
                  <label for="checkbox{{item.id}}"></label>
                </div>
              </td>
              <td><a href="{{ url_for('article.index', company_id=item.id) }}">{{ item.name }}</a></td>
              <td>
              <span class="label label-table label-primary">{{ item.get_hired_count() + item.get_not_hired_count() }}</span></td>
              <td><span class="label label-success">{{ item.get_hired_count() }}</span></td>
              <td><span class="label label-warning">{{ item.get_not_hired_count() }}</span></td>
              <td>
                积分: {{ item.get_hired_count() * 2 + item.get_not_hired_count() * 1 }}
              </td>
              <td class="text-nowrap">
                <div style="display: flex">
                  <a href="{{ url_for('companys.update', id=item.id) }}" data-toggle="tooltip" data-original-title="编辑">
                    <i class="fa fa-pencil text-inverse m-r-10"></i> </a>
                  <a href="javascript:void(0)" onclick="sa_warning(event)" data-toggle="tooltip" data-original-title="删除">
                    <span class="fa fa-close text-danger" data-id="{{ item.id }}"></span>
                  </a>
                </div>
              </td>
            </tr>
            {% endfor %}
            {% endif %}
          </tbody>
        </table>
        </div>
      </div>
    </div>

  </div>


</div>
{% endblock %}

{% block scripts %}
{{ super() }}
<script src="{{ url_for('static', filename='assets/plugins/datatables/jquery.dataTables.min.js') }}"></script>
<script src="https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>

<script type="text/javascript">
  $(function () {
    $('#check_box').click(function () {
      $("input[name='ids[]']").prop('checked', this.checked)
    })
  })
  function submit_delete () {
    let chk_value = [];
    $("input[name='ids[]']:checked").each(function () {
      chk_value.push($(this).val())
    })
    if (chk_value.length === 0) {
      swal("没有选择单位!")
      return false
    }
    swal({
      title: "您确定?",
      text: "您确定要删除这单位吗!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "是的, 我确定!",
      cancelButtonText: "取消操作"
    }, function () {
      $.post({
        url: "{{ url_for('.ajax_delete') }}",
        data: {
          'csrf_token': '{{ csrf_token() }}',
          'ids': JSON.stringify(chk_value)
        },
        success: function (result) {
          if (result.success) {
            window.location.reload()
          }
          if (result.success === false) {
            alert(result.message)
          }
        }
      })
    })
  }
  function choose () {
    let check_box = $('#check_box')
    check_box.prop('checked', !check_box.is(':checked'))
    $("input[name='ids[]']").prop('checked', check_box.is(':checked'))
  }
  function sa_warning(e) {
    swal({
      title: "您确定?",
      text: "您确定要删除这个单位吗!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "是的, 我确定!",
      cancelButtonText: "取消操作",
      closeOnConfirm: false
    }, function () {
      window.location.href='/companys/delete/' + e.srcElement.dataset['id']
    });
  }
  $('#dataTable').DataTable({
    "order": [[ 2, 'desc' ]],
    "columnDefs": [{ "orderable": false, "targets": 0 },{ "orderable": false, "targets": 6 }],
    "oLanguage": {
      "sLengthMenu": "每页显示 _MENU_ 条记录",
      "sZeroRecords": "对不起，查询不到任何相关数据",
      // "sInfo": "ad当前显示 _START_ 到 _END_ 条，共 _TOTAL_条记录",
      "sInfo": `
        <div class="btn-group m-b-10 m-r-10" role="group">
        <button type="button" onclick="choose()" class="btn btn-secondary">全选/取消</button> 
        <button type="button" onclick="submit_delete()" class="btn waves-effect waves-light btn-danger">删除</button>
        </div>
      `,
      "sInfoEmtpy": "找不到相关数据",
      "sInfoFiltered": "数据表中共为 _MAX_ 条记录)",
      "sProcessing": "正在加载中...",
      "sSearch": "搜索",
      "oPaginate": {
        "sFirst": "第一页",
        "sPrevious": " 上一页 ",
        "sNext": " 下一页 ",
        "sLast": " 最后一页 "
      }
    }
  });

</script>
{% endblock scripts %}